'use strict';

define(['controllers', '../service/statService', 'datepicker'], function (controllers) {
    controllers.stat.register('FilterController', ['$scope', '$rootScope', 'StatService', function ($scope, $rootScope, statService) {
        $scope.init = function () {

            $scope.query_nodeId = '';
            $scope.query_collectionId = '';
            $scope.query_replicaId = '';
            $scope.query_statType = $scope.STAT_TYPE_HOUR;

            $scope.query_startTime = new Date().getDayBeginTime();
            $scope.query_endTime = new Date().getDayEndTime();

            if ($scope.onlyNode) {
                $scope.query_dimension = $scope.DIMENSION_NODE;
                $scope.dimensions = [$scope.DIMENSION_NODE];
            } else {
                $scope.query_dimension = $scope.DIMENSION_COLLECTION;
                $scope.dimensions = [$scope.DIMENSION_COLLECTION, $scope.DIMENSION_NODE];
            }
            $scope.dimensionMap = {};
            $scope.dimensionMap[$scope.DIMENSION_COLLECTION] = '统计Solr集合';
            $scope.dimensionMap[$scope.DIMENSION_NODE] = '统计Solr节点';
            $scope.dimensionMap[$scope.DIMENSION_REPLICA] = '统计Solr副本';

            $scope.nodes = statService.findNodes(function (nodes) {
                $scope.nodeMap = {};
                for (var i = 0; i < nodes.length; i++) {
                    $scope.nodeMap[nodes[i].id] = nodes[i];
                }
            });

            $scope.collections = statService.findCollections(function (collections) {
                $scope.collectionMap = {};
                for (var i = 0; i < collections.length; i++) {
                    $scope.collectionMap[collections[i].id] = collections[i];
                }
            });

            $('#query_startTime').val(new Date($scope.query_startTime).format($scope.DATE_FORMAT));
            $('#query_endTime').val(new Date($scope.query_endTime).format($scope.DATE_FORMAT));
        };

        $scope.initDatepicker = function() {
            $('#query_startTime').datepicker({
                format: 'yyyy-mm-dd'
            }).on('changeDate', function (e) {
                var oldStartTime = $scope.query_startTime;
                var newStartTime = e.date.getDayBeginTime();
                if (newStartTime > $scope.query_endTime) {
                    $('#query_startTime').val(new Date(oldStartTime).format($scope.DATE_FORMAT));
                    return false;
                }
                $scope.query_startTime = newStartTime;
            });

            $('#query_endTime').datepicker({
                format: 'yyyy-mm-dd'
            }).on('changeDate', function (e) {
                var oldEndTime = $scope.query_endTime;
                var newEndTime = e.date.getDayEndTime();
                if (newEndTime < $scope.query_startTime) {
                    $('#query_endTime').val(new Date(oldEndTime).format($scope.DATE_FORMAT));
                    return false;
                }
                $scope.query_endTime = newEndTime;
            });
        };

        $scope.initEvent = function () {
            $scope.$watch('query_nodeId', function () {
                $scope.collections = statService.findCollections({nodeId: $scope.query_nodeId});
                $scope.findReplicas();
                $scope.reloadDimension();
                $scope.query_replicaId = '';
            });
            $scope.$watch('query_collectionId', function () {
                $scope.nodes = statService.findNodes({collectionId: $scope.query_collectionId});
                $scope.findReplicas();
                $scope.reloadDimension();
                $scope.query_replicaId = '';
            });
            $scope.$watch('query_replicaId', function () {
                $scope.reloadDimension();
            });
        };

        $scope.initHighchart = function () {
            Highcharts.setOptions({ global: { useUTC: false } });
        };

        $scope.findReplicas = function () {
            $scope.replicas = statService.findReplicas({
                nodeId: $scope.query_nodeId,
                collectionId: $scope.query_collectionId
            }, function (replicas) {
                $scope.replicaMap = {};
                for (var i = 0; i < replicas.length; i++) {
                    $scope.replicaMap[replicas[i].id] = replicas[i];
                }
            });
        };

        $scope.reloadDimension = function () {
            if ($scope.onlyNode) {
                $scope.dimensions = [$scope.DIMENSION_NODE];
                $scope.query_dimension = $scope.DIMENSION_NODE;
            } else {
                if (isEmpty($scope.query_nodeId) && isEmpty($scope.query_collectionId) && isEmpty($scope.query_replicaId)) {
                    if ($scope.query_dimension == null || $scope.query_dimension == $scope.DIMENSION_REPLICA) {
                        $scope.query_dimension = $scope.DIMENSION_COLLECTION;
                    }
                    $scope.dimensions = [$scope.DIMENSION_COLLECTION, $scope.DIMENSION_NODE];
                } else {
                    $scope.query_dimension = $scope.DIMENSION_REPLICA;
                    $scope.dimensions = [$scope.DIMENSION_REPLICA];
                }
            }
        };

        $scope.query = function () {
            if ($scope.query_dimension == $scope.DIMENSION_REPLICA && $scope.replicas.length == 0) {
                alert('Solr副本不存在');
                return false;
            }
            $scope.getStat({
                dimension: $scope.query_dimension,
                nodeId: $scope.query_nodeId,
                collectionId: $scope.query_collectionId,
                replicaId: $scope.query_replicaId,
                statType: $scope.query_statType,
                startTime: $scope.query_startTime,
                endTime: $scope.query_endTime
            });
        };

        $scope.reset = function () {
            $scope.init();
            $scope.query();
        };

        $scope.init();
        $scope.initDatepicker();
        $scope.initEvent();
        $scope.initHighchart();
        $scope.query();
    }]);
});